import {Block, View} from '@tarojs/components'
import React, { useEffect, useState } from 'react';
import './join-tt.scss'

const JoinTt: React.FC = () => {
  const [show, setShow] = useState(false);

  useEffect(() => {
    tt.checkFollowAwemeState({
      success: (res) => {
        if (!res.hasFollowed) {
          setShow(true)
        }
      },
      fail: (res) => {
        let flag = true
        const errMsg = res.errMsg
        if (errMsg.endsWith('feature is not supported in app')) { // 宿主未实现
          flag = false
        }
        if (errMsg.endsWith('aweme uid is null')) { // 没有绑定想要跳转的抖音号
          flag = false
        }
        console.log(res)
        setShow(flag)
      }
    })
  }, [])

  const goFollowAwemeUser = async () => {
    tt.followAwemeUser({
      success: (res) => {
        setShow(false)
      },
      fail: (res) => {
        console.log(res)
      }
    })
  }

  return (
    <Block>
      {show && <View className='join-tt' onClick={goFollowAwemeUser}>关注有礼</View>}
    </Block>
  )
}
export default JoinTt
